
Imports System
Imports System.Collections
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Web
Imports System.Web.SessionState
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.HtmlControls
Imports _2bMatesSite.BO

Namespace _2bMatesSite
	''' <summary>
	''' Summary description for post_bulletin.
	''' </summary>
	Public Partial Class post_bulletin
		Inherits BaseWebPage
		Private action As Integer = -1
		Protected isGroupMode As Boolean = False
		Protected isModerator As Boolean = False
		Protected grpID As Integer = -1

		Protected grp As Group = Nothing
		Protected htPageConstants As New Hashtable()

		Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
			' Put user code to initialize the page here
			' this.pageTemplate.PageTitle = PageTitles.POST_BULLETIN_PAGE;

			Dim usr As User = LoggedUser
			lblNoFriends.Visible = False
			If usr = Nothing Then
				Common.Utils.RedirectToLoginPage(Page)
			End If

			' init page from XML
			Me.initPageTitles(Constants.POST_BULLETIN, "", Form1, Nothing, htPageConstants)
			Me.initAdvertisements(Form1, Nothing)

			' init tabs
			ucTabs.ActiveTab = 0
			ucTabs.PageName = Constants.POST_BULLETIN
			grpBlogTabs.ActiveTab = 1
			grpBlogTabs.PageName = Constants.POST_BULLETIN

			' check if a group param is given  
			If Request.Params(Constants.GROUP_PARAM) <> Nothing AndAlso Request.Params(Constants.GROUP_PARAM) <> "" Then
				isGroupMode = True
				Try
					grpID = Convert.ToInt32(Request.Params(Constants.GROUP_PARAM))
				Catch
					Throw New Exception(Constants.ERROR_MESSAGE_MISSING_PARAMETERS)
				End Try

				grp = Groups.GetGroupByID(grpID)
				If grp = Nothing Then
					Throw New Exception(Constants.ERROR_MESSAGE_GROUP_DOESNT_EXIST_ERROR)
				End If

				If Not grp.Members.Contains(usr.ID) Then
					Throw New Exception(Constants.ERROR_MESSAGE_ONLY_MEMBER_CAN_VIEW_MESSAGES)
				End If

				' set menu type to group
				litMainPageTitle.Text = Me.GetPageConstText(htPageConstants, "CONST_PageTitle_TEXT")
				ucTabs.Visible = False
				grpBlogTabs.Visible = True

				' set return hyperlink for a group
				hlReturnToBulletinList.NavigateUrl = "../" + Constants.BULLETIN + "?" + Constants.GROUP_PARAM + "=" + grp.ID
				hlReturnToBulletinList.Text = Me.GetPageConstText(htPageConstants, "CONST_ReturnToGroupBulletin_TEXT")
				btnPost.Text = Me.GetPageConstText(htPageConstants, "CONST_PostGroupBulletin_TEXT")
				hlBackToGroup.Text = (Me.GetPageConstText(htPageConstants, "CONST_GroupBackLink_TEXT")).Replace("[PARAM]", grp.Name)
				hlBackToGroup.NavigateUrl = "../" + Constants.GROUP + "?" + Constants.GROUP_PARAM + "=" + grp.ID
			Else
				hlBackToGroup.Visible = False
			End If

			' set menu type to user
			If Not isGroupMode Then
				ucTabs.Visible = True
				grpBlogTabs.Visible = False

				' set return hyperlink for a user
				hlReturnToBulletinList.NavigateUrl = "../" + Constants.INBOX

				If Not IsPostBack Then
					Try
						If Request.Params("action") <> Nothing AndAlso Request.Params("action") <> "" Then
							action = Convert.ToInt32(Request.Params("action"))
						End If
					Catch
						Throw New Exception(Constants.ERROR_MESSAGE_MISSING_PARAMETERS)
					End Try


					If action = Constants.BULLETIN_SEND_GAME_ACTION Then
						txtBulletinSubject.Text = Constants.BULLETIN_SEND_GAME_SUBJECT
						txtBulletinBody.Text = Request.ServerVariables("HTTP_REFERER")
						txtBulletinBody.CssClass = "blue_bulletin"
					End If
				End If
			Else
				'hlRightLink.Text = (this.GetPageConstText(htPageConstants, "CONST_GroupBackLink_TEXT")).Replace("[PARAM]",  grp.Name);
				'hlRightLink.NavigateUrl = "../" + Constants.GROUP + "?" + Constants.GROUP_PARAM + "=" + grp.ID;
				lblRecipients.Text = Me.GetPageConstText(htPageConstants, "CONST_BulExplain_TEXT")
			End If

			Dim showMessageBox As Boolean = True
			If (isGroupMode AndAlso grp.Members.Count = 0) OrElse (Not isGroupMode AndAlso usr.DirectFriends.Count = 0) Then
				showMessageBox = False
				lblNoFriends.Visible = True
			End If

			If Not showMessageBox Then
				txtBulletinSubject.Visible = False
				rvBulletinSubject.Visible = False
				rvBulletinBody.Visible = False
				btnSendBulletin.Visible = False
				ValidationSummary1.Visible = False
				lblMessage.Visible = False
				lblEmail.Visible = False
				lblSubject.Visible = False
				lblRecipients.Visible = False
				txtBulletinBody.Visible = False
				If Not isGroupMode Then
					lblNoFriends.Visible = True
				End If
			End If

			' limit textarea
			txtBulletinBody.Attributes.Add("onKeyDown", Constants.ON_KEY_DOWN_SCRIPT.Replace(Constants.TEXT_AREA_MAX_CHARS_TAG, Constants.TEXT_AREA_LONG_MESSAGES_MAX_CHARS.ToString()))
			txtBulletinBody.Attributes.Add("onKeyUp", Constants.ON_KEY_UP_SCRIPT.Replace(Constants.TEXT_AREA_MAX_CHARS_TAG, Constants.TEXT_AREA_LONG_MESSAGES_MAX_CHARS.ToString()))
		End Sub

		#region Web Form Designer generated code
		Protected Overloads Overrides Sub OnInit(ByVal e As EventArgs)
			'
			' CODEGEN: This call is required by the ASP.NET Web Form Designer.
			'
			InitializeComponent()
			MyBase.OnInit(e)
		End Sub

		''' <summary>
		''' Required method for Designer support - do not modify
		''' the contents of this method with the code editor.
		''' </summary>
		Private Sub InitializeComponent()
			AddHandler Me.cvHtmlInSubject.ServerValidate, AddressOf Me.cvHtmlInSubject_ServerValidate
		End Sub
#End Region

		Protected Sub btnSendBulletin_Click(ByVal sender As Object, ByVal e As System.EventArgs)
			If LoggedUser = Nothing Then
				Common.Utils.RedirectToLoginPage(Page)
			End If

			If Page.IsValid Then
				DoSendBulletin()
			End If
		End Sub

		Private Sub DoSendBulletin()
			' Send a bulletin
			Dim subject As String = ""
			Dim body As String = ""

			subject = txtBulletinSubject.Text.Trim()
			body = txtBulletinBody.Text.Trim()

			subject = removeScriptTags(subject.Substring(0, Math.Min(subject.Length, Constants.TEXT_FIELD_DEFAULT_MAX_CHARS)))
			body = removeScriptTags(body.Substring(0, Math.Min(body.Length, Constants.TEXT_AREA_LONG_MESSAGES_MAX_CHARS)))

			lblBulletin.Text = "<span class='main_text_opposite_bold'>Subject:</span> " + subject + Environment.NewLine + Environment.NewLine + "<span class='main_text_opposite_bold'>Body:</span> " + body

			'if ( isGroupMode )
			'{
			'GroupBulletinMessage bltNew = new GroupBulletinMessage( LoggedUser.ID, grpID, subject, body );
			'bltNew.Send();
			'Response.Redirect( Constants.BULLETIN + "?" + Constants.GROUP_PARAM + "=" + grp.ID );
			'}
			'else
			'{
			'BulletinMessage bltNew = new BulletinMessage( LoggedUser.ID, subject, body );
			'bltNew.Send();
			'Response.Redirect( Constants.BULLETIN );
			'}
			trPostBulletin.Visible = False
			trConfrimation.Visible = True
		End Sub

		Protected Sub btnCancel_Click(ByVal sender As Object, ByVal e As System.EventArgs)
			If isGroupMode Then
				Response.Redirect(Constants.BULLETIN + "?" + Constants.GROUP_PARAM + "=" + grp.ID)
			Else
				Response.Redirect(Constants.BULLETIN)
			End If
		End Sub

		Protected Sub btnPost_Click(ByVal sender As Object, ByVal e As System.EventArgs)
			Dim subject As String = ""
			Dim body As String = ""

			subject = txtBulletinSubject.Text.Trim()
			body = txtBulletinBody.Text.Trim()

			subject = removeScriptTags(subject.Substring(0, Math.Min(subject.Length, Constants.TEXT_FIELD_DEFAULT_MAX_CHARS)))
			body = removeScriptTags(body.Substring(0, Math.Min(body.Length, Constants.TEXT_AREA_LONG_MESSAGES_MAX_CHARS)))


			If isGroupMode Then
				Dim bltNew As New GroupBulletinMessage(LoggedUser.ID, grpID, subject, body)
				bltNew.Send()
				Response.Redirect(Constants.BULLETIN + "?" + Constants.GROUP_PARAM + "=" + grp.ID)
			Else
				Dim bltNew As New BulletinMessage(LoggedUser.ID, subject, body)
				bltNew.Send()
				Response.Redirect(Constants.BULLETIN)
			End If
		End Sub

		Protected Sub btnEdit_Click(ByVal sender As Object, ByVal e As System.EventArgs)
			trPostBulletin.Visible = True
			trConfrimation.Visible = False
		End Sub

		Private Sub cvHtmlInSubject_ServerValidate(ByVal source As Object, ByVal args As System.Web.UI.WebControls.ServerValidateEventArgs)
			Dim fn As String = Request.Form(txtBulletinSubject.UniqueID)

			If isHTML(fn) Then
				args.IsValid = False
			Else
				args.IsValid = True
			End If
		End Sub

		====================
		' This public property was added by conversion wizard to allow the access of a protected, autogenerated member variable Form1.
		====================
		Public ReadOnly Property Form1() As System.Web.UI.HtmlControls.HtmlForm
			Get
				Return Migrated_Form1
			End Get
		End Property
		'set { Migrated_Form1 = value; }
	End Class
End Namespace






